﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using StateGrid.MDBservice;
using System.IO;
using iTextSharp.text.pdf;

namespace StateGrid.ZWHY
{
    class ZWHY_MDB : MDBservice.MDBservice
    {
        public override String id
        {
            get
            {
                return "zhongwenhuiyi";
            }
        }

        private ZWHY_DAO zwhyDAO = new ZWHY_DAO();
        
        public override void clear()
        {
            base.clear();
        }

        public override String getTest()
        {
            IList<ZWHY_FL> r = zwhyDAO.getAllZwhyFL();
            String ret = String.Empty;
            foreach (ZWHY_FL i in r)
            {
                ret += i.ToString();
            }
            return ret;
        }

        public ZWHY_MDB()
        {
            pmTableName = "ZWHY_WF";
            formatPmTableName = "ZWHY";
            symlTableName = "ZWHY_SYML";
            title = "中文会议";
        }

        public override void setColumnList()
        {
            this.ColumnList = new List<Column>();
            if (hasFormatTable)
            {
                this.ColumnList.Add(new Column("索引编码", OleDbType.WChar, 20));
                this.ColumnList.Add(new Column("文件名", OleDbType.WChar, 30));
                this.ColumnList.Add(new Column("中文篇名", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("外文篇名", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("作者", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("作者单位", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("中文文摘", OleDbType.WChar, 0));
                this.ColumnList.Add(new Column("外文文摘", OleDbType.WChar, 0));
                this.ColumnList.Add(new Column("中文主题词", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("外文主题词", OleDbType.WChar, 255));
                this.ColumnList.Add(new Column("页数", OleDbType.SmallInt));
                this.ColumnList.Add(new Column("国图分类", OleDbType.WChar, 30));
                this.ColumnList.Add(new Column("馆藏号", OleDbType.WChar, 250));
                this.ColumnList.Add(new Column("原文件名", OleDbType.WChar, 250));
                this.ColumnList.Add(new Column("登录人", OleDbType.WChar, 4));
                this.ColumnList.Add(new Column("更新日期", OleDbType.Date));
                this.ColumnList.Add(new Column("更新项", OleDbType.WChar, 10));
            }
            else
            {
                //TODO:
                this.ColumnList.Add(new Column("会议名称", OleDbType.WChar, 0)); //原文件名 15
                this.ColumnList.Add(new Column("文件名", OleDbType.WChar, 0)); //原文件名   41
                this.ColumnList.Add(new Column("论文标题", OleDbType.WChar, 0));      //65
                this.ColumnList.Add(new Column("英文论文题名", OleDbType.WChar, 0));     //182
                this.ColumnList.Add(new Column("论文作者", OleDbType.WChar, 0));          //54
                this.ColumnList.Add(new Column("作者机构", OleDbType.WChar, 0));    //0
                //this.ColumnList.Add(new Column("开本", OleDbType.WChar, 4));  //如何算？？
                this.ColumnList.Add(new Column("页码", OleDbType.WChar, 0));     //49
                this.ColumnList.Add(new Column("论文摘要", OleDbType.WChar, 0));      //0
                this.ColumnList.Add(new Column("英文摘要", OleDbType.WChar, 0));      //200
                this.ColumnList.Add(new Column("论文关键词", OleDbType.WChar, 0));    //81
                this.ColumnList.Add(new Column("英文论文关键词", OleDbType.WChar, 0)); //125
                this.ColumnList.Add(new Column("分类号", OleDbType.WChar, 0));    //26
                //this.ColumnList.Add(new Column("文献标识", OleDbType.WChar, 4));
                //this.ColumnList.Add(new Column("登录人", OleDbType.WChar, 4));
                //this.ColumnList.Add(new Column("更新日期", OleDbType.Date));
            }
            this.symlColumnList = new List<Column>();
            //索引目录表列名
            this.symlColumnList.Add(new Column("分类编码", OleDbType.WChar, 10));
            this.symlColumnList.Add(new Column("定期会议", OleDbType.WChar, 50));
            this.symlColumnList.Add(new Column("索引编码", OleDbType.WChar, 20));
            this.symlColumnList.Add(new Column("文种", OleDbType.WChar, 4));
            this.symlColumnList.Add(new Column("年份", OleDbType.WChar, 4));
            this.symlColumnList.Add(new Column("中文名称", OleDbType.WChar, 200));
            this.symlColumnList.Add(new Column("外文名称", OleDbType.WChar, 255));
            this.symlColumnList.Add(new Column("主办单位", OleDbType.WChar, 200));
            this.symlColumnList.Add(new Column("承办单位", OleDbType.WChar, 100));
            this.symlColumnList.Add(new Column("出版单位", OleDbType.WChar, 150));
            this.symlColumnList.Add(new Column("发布日期", OleDbType.Date));
            this.symlColumnList.Add(new Column("国图分类", OleDbType.WChar, 30));
            this.symlColumnList.Add(new Column("会议地点", OleDbType.WChar, 50));
            this.symlColumnList.Add(new Column("简介", OleDbType.WChar, 255));
            this.symlColumnList.Add(new Column("馆藏号", OleDbType.WChar, 250));
            this.symlColumnList.Add(new Column("登录日期", OleDbType.Date));

        }

        protected override PM getPM(OleDbDataReader reader, out String err)
        {
            err = String.Empty;
            ZWHY_PM pm = new ZWHY_PM();
            if (hasFormatTable)
            {
                pm.sybm = reader["索引编码"].ToString().Trim();
                pm.wjm = reader["文件名"].ToString().Trim();
                pm.zwpm = reader["中文篇名"].ToString().Trim();
                pm.wwpm = reader["外文篇名"].ToString().Trim();
                pm.zz = reader["作者"].ToString().Trim();
                pm.zzdw = reader["作者单位"].ToString().Trim();
                pm.zwwz = reader["中文文摘"].ToString().Trim();
                pm.wwwz = reader["外文文摘"].ToString().Trim();
                pm.zwztc = reader["中文主题词"].ToString().Trim();
                pm.wwztc = reader["外文主题词"].ToString().Trim();
                try
                {
                    pm.ys = int.Parse(reader["页数"].ToString());
                }
                catch (Exception)
                {
                    pm.ys = 0;
                    err += pm.wjm + ":" + pm.zwpm + ": 页数\"" + reader["页数"] + "\"出错，置为0\n";
                }
                pm.gtfl = reader["国图分类"].ToString().Trim();
                pm.gch = reader["馆藏号"].ToString().Trim();
                pm.ywjm = reader["原文件名"].ToString().Trim();
            }
            else
            {
                //没有用到这个分支
                //pm.sybm = reader["索引编码"].ToString();
                //pm.wjm = reader["文件名"].ToString();
                pm.zwpm = reader["论文标题"].ToString();
                pm.wwpm = reader["英文论文题名"].ToString();
                pm.zz = reader["论文作者"].ToString();
                pm.zzdw = reader["作者机构"].ToString();
                //pm.kb = reader["开本"].ToString();
                pm.zwwz = reader["论文摘要"].ToString();
                pm.wwwz = reader["英文论文摘要"].ToString();
                pm.zwztc = reader["论文关键词"].ToString();
                pm.wwztc = reader["英文论文关键词"].ToString();
                pm.gtfl = reader["分类号"].ToString();
                //pm.wxbs = reader["文献标识"].ToString();
                pm.ywjm = reader["文件名"].ToString();
            }
            pm.dlr = user.name;
            pm.fillEmptyStringAsNull();
            return pm;
        }

        protected override SYML getSyml(OleDbDataReader reader, out string err)
        {
            err = String.Empty;
            ZWHY_SYML syml = new ZWHY_SYML();
            syml.flbm = reader["分类编码"].ToString().Trim();
            syml.dqhy = reader["定期会议"].ToString().Trim();
            syml.sybm = reader["索引编码"].ToString().Trim();
            syml.wz = reader["文种"].ToString().Trim();
            try
            {
                syml.nf = int.Parse(reader["年份"].ToString());
            }
            catch (Exception)
            {
                syml.nf = 0;
                err += syml.flbm + ":" + syml.zwmc + ": 年份\"" + reader["年份"] + "\"出错，置为0\n";
            }
            syml.zwmc = reader["中文名称"].ToString().Trim();
            syml.wwmc = reader["外文名称"].ToString().Trim();
            syml.zbdw = reader["主办单位"].ToString().Trim();
            syml.cbdw = reader["承办单位"].ToString().Trim();
            syml.chbdw = reader["出版单位"].ToString().Trim();
            try
            {
                if (String.Empty.Equals(reader["发布日期"].ToString().Trim()))
                {
                    err += syml.flbm + ":" + syml.zwmc + ":发布日期为空\n";
                    syml.fbrq = DateTime.MinValue;
                }
                else
                    syml.fbrq = DateTime.Parse(reader["发布日期"].ToString().Trim());
            }
            catch
            {
                err += syml.flbm + ":" + syml.zwmc + ": 发布日期\"" + reader["发布日期"] + "\"出错\n";
            }
            syml.gtfl = reader["国图分类"].ToString().Trim();
            syml.hydd = reader["会议地点"].ToString().Trim();
            syml.jj = reader["简介"].ToString().Trim();
            syml.gch = reader["馆藏号"].ToString().Trim();
            syml.dlrq = DateTime.Now;
            syml.fillEmptyStringAsNull();
            return syml;
        }

        public override bool checkPMValid(PM pm, out string errMsg)
        {
            errMsg = String.Empty;
            ZWHY_PM zwhy = (ZWHY_PM)pm;
            this.openOleDbConnection();
            sqlcmd.CommandText = @"select * from " + formatPmTableName + " where 索引编码=@索引编码 and 中文篇名=@中文篇名";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("索引编码", zwhy.sybm));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwhy.zwpm));
            OleDbDataReader reader = sqlcmd.ExecuteReader();

            if (reader.Read())
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return true;
            }
            else
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return false;
            }
            //            this.closeOleDbConnection();
        }

        public override bool checkSymlValid(SYML syml, out string errMsg)
        {
            errMsg = String.Empty;
            ZWHY_SYML zwhy = (ZWHY_SYML)syml;
            this.openOleDbConnection();
            sqlcmd.CommandText = @"select * from " + symlTableName + " where 分类编码=@分类编码 and 中文名称=@中文名称 and 馆藏号=@馆藏号";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", zwhy.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("中文名称", zwhy.zwmc));
            sqlcmd.Parameters.Add(new OleDbParameter("馆藏号", zwhy.gch));
            OleDbDataReader reader = sqlcmd.ExecuteReader();

            if (reader.Read())
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return true;
            }
            else
            {
                reader.Close();
                sqlcmd.Parameters.Clear();
                return false;
            }
        }
        public override String getHash()
        {
            String err = String.Empty;
            IList<ZWHY_SYML> temp = null;
            try
            {
                temp = zwhyDAO.getAllZwhySYML();
            }
            catch (Exception)
            {
                err = "错误：数据库索引目录表无项目\n";
                return err;
            }
            if (Hash == null)
                Hash = new Hashtable();
            Hash.Clear();
            foreach (ZWHY_SYML syml in temp)
            {
                Hash.Add(syml.sybm, syml);
            }
            return err;
        }
        public override string formatData(PM pm)
        {
            //反馈信息
            String result = String.Empty;
            String Empty = "NULL";
            ZWHY_PM zwhy = (ZWHY_PM)pm;

            zwhy.zz = GlobalMethod.Semicolon(zwhy.zz);
            zwhy.zzdw = GlobalMethod.Semicolon(zwhy.zzdw);
            zwhy.zwztc = GlobalMethod.Semicolon(zwhy.zwztc);
            zwhy.wwztc = GlobalMethod.Semicolon(zwhy.wwztc);
            zwhy.gtfl = GlobalMethod.Semicolon(zwhy.gtfl);
            zwhy.wjm = "自动生成";
            zwhy.dlr = user.name;
            zwhy.gxrq = DateTime.Now;
            zwhy.gxx = "";
            try
            {
                ZWHY_SYML tempSyml = zwhyDAO.getSingleZwhySYML(zwhy.gch);
                zwhy.sybm = tempSyml.sybm;
            }
            catch (Exception)
            {
                result += "馆藏号\"" + zwhy.gch + "\"出错";
            }
            if (Empty.Equals(zwhy.ywjm))
            {
                result += "原文件名（PDF全文）为空 ";
            }
            else
            {
                if (!File.Exists(fileFolder + zwhy.ywjm))
                {
                    result += zwhy.ywjm + " PDF文件不存在";
                }
                else
                {
                    PdfReader pdf = new PdfReader(fileFolder + zwhy.ywjm);
                    if (null != pdf)
                    {
                        zwhy.ys = pdf.NumberOfPages;
                    }
                    else
                    {
                        zwhy.ys = 0;
                    }
                    pdf.Close();
                }
            }
            if (Empty.Equals(zwhy.zwpm))
            {
                result += "中文篇名（论文标题）为空 ";
            }
            if (!result.Equals(String.Empty))
            {
                result += " : 《" + zwhy.zwpm + "》\n";
            }

            return result;
        }

        public override string formatSyml(SYML syml)
        {
            //反馈信息
            String result = String.Empty;
            String Empty = "NULL";
            ZWHY_SYML zwhy = (ZWHY_SYML)syml;

            zwhy.wz = "中";
            zwhy.zbdw = GlobalMethod.Semicolon(zwhy.zbdw);
            zwhy.cbdw = GlobalMethod.Semicolon(zwhy.cbdw);
            zwhy.chbdw = GlobalMethod.Semicolon(zwhy.chbdw);
            zwhy.gtfl = GlobalMethod.Semicolon(zwhy.gtfl);
            zwhy.sybm = "自动生成";
            zwhy.dlrq = DateTime.Now;

            if (Empty.Equals(zwhy.zwmc))
            {
                result += "中文名称为空 ";
            }
            if (Empty.Equals(zwhy.gch))
            {
                result += "馆藏号为空 ";
            }
            if (!result.Equals(String.Empty))
            {
                result += " : 《" + zwhy.ToString() + "》\n";
            }

            return result;
        }

        public override String insertPM(PM pm)
        {
            ZWHY_PM zwhy = (ZWHY_PM)pm;
            int back;
            String err = String.Empty;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(索引编码 char(20), 文件名 char(30), 中文篇名 char(255), 外文篇名 char(255), 作者 char(255), 作者单位 char(255), 中文文摘 text, 外文文摘 text, 中文主题词 char(255), 外文主题词 char(255), 页数 smallInt, 国图分类 char(30), 馆藏号 char(250), 原文件名 char(250), 登录人 char(4), 更新日期 datetime), 更新项 char(10)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return err;
                }
            }

            //sql语句
            /*            sqlcmd.CommandText = @"insert into " + formatTableName + @"(索引编码,文件名,原文件名,年份,期号,中文篇名,外文篇名,作者,作者单位,开本,页数,中文文摘,外文文摘,中文主题词,外文主题词,国图分类,文献标识,登录人,更新日期) values(@索引编码,@文件名,@原文件名,@年份,@期号,@中文篇名,@外文篇名,@作者,@作者单位,@开本,@页数,@中文文摘,@外文文摘,@中文主题词,@外文主题词,@国图分类,@文献标识,@登录人,@更新日期)";
                        sqlcmd.Connection = this.ObjConnection;
                        sqlcmd.Parameters.Add(new OleDbParameter("索引编码", zwqk.sybm));
                        sqlcmd.Parameters.Add(new OleDbParameter("文件名", zwqk.wjm));
                        sqlcmd.Parameters.Add(new OleDbParameter("原文件名", zwqk.ywjm));
                        sqlcmd.Parameters.Add(new OleDbParameter("年份", zwqk.nf));
                        sqlcmd.Parameters.Add(new OleDbParameter("期号", zwqk.qh));
                        sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwqk.zwpm));
                        sqlcmd.Parameters.Add(new OleDbParameter("外文篇名", zwqk.wwpm));
                        sqlcmd.Parameters.Add(new OleDbParameter("作者", zwqk.zz));
                        sqlcmd.Parameters.Add(new OleDbParameter("作者单位", zwqk.zzdw));
                        sqlcmd.Parameters.Add(new OleDbParameter("开本", zwqk.kb));
                        sqlcmd.Parameters.Add(new OleDbParameter("页数", zwqk.ys));
                        sqlcmd.Parameters.Add(new OleDbParameter("中文文摘", zwqk.zwwz));
                        sqlcmd.Parameters.Add(new OleDbParameter("外文文摘", zwqk.wwwz));
                        sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zwqk.zwztc));
                        sqlcmd.Parameters.Add(new OleDbParameter("外文主题词", zwqk.wwztc));
                        sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zwqk.gtfl));
                        sqlcmd.Parameters.Add(new OleDbParameter("文献标识", zwqk.wxbs));
                        sqlcmd.Parameters.Add(new OleDbParameter("登录人", zwqk.dlr));
                        sqlcmd.Parameters.Add(new OleDbParameter("更新日期", zwqk.gxrq));
                        */
            sqlcmd.CommandText = @"insert into " + formatPmTableName + @"(索引编码,文件名,中文篇名,外文篇名,作者,作者单位,中文文摘,外文文摘,中文主题词,外文主题词,页数,国图分类,馆藏号,原文件名,登录人,更新日期,更新项) values(@索引编码,@文件名,@中文篇名,@外文篇名,@作者,@作者单位,@中文文摘,@外文文摘,@中文主题词,@外文主题词,@页数,@国图分类,@馆藏号,@原文件名,@登录人,@更新日期,@更新项)";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("索引编码", zwhy.sybm));
            sqlcmd.Parameters.Add(new OleDbParameter("文件名", zwhy.wjm));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwhy.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("外文篇名", zwhy.wwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("作者", zwhy.zz));
            sqlcmd.Parameters.Add(new OleDbParameter("作者单位", zwhy.zzdw));
            sqlcmd.Parameters.Add(new OleDbParameter("中文文摘", zwhy.zwwz));
            sqlcmd.Parameters.Add(new OleDbParameter("外文文摘", zwhy.wwwz));
            sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zwhy.zwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("外文主题词", zwhy.wwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("页数", zwhy.ys));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zwhy.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("馆藏号", zwhy.gch));
            sqlcmd.Parameters.Add(new OleDbParameter("原文件名", zwhy.ywjm));
            sqlcmd.Parameters.Add(new OleDbParameter("登录人", zwhy.dlr));
            OleDbParameter parameter = new OleDbParameter("更新日期", OleDbType.DBDate);
            parameter.Value = zwhy.gxrq;
            sqlcmd.Parameters.Add(parameter);
            sqlcmd.Parameters.Add(new OleDbParameter("更新项", zwhy.gxx));
            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();

            this.closeOleDbConnection();
            return err;
        }

        public override String insertSyml(SYML syml)
        {
            ZWHY_SYML zwhy = (ZWHY_SYML)syml;
            int back;
            String err = String.Empty;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();

            sqlcmd.CommandText = @"insert into " + symlTableName + @"(分类编码,定期会议,索引编码,年份,文种,中文名称,外文名称,主办单位,承办单位,出版单位,发布日期,国图分类,会议地点,简介,馆藏号,登录日期) values(@分类编码,@定期会议,@索引编码,@年份,@文种,@中文名称,@外文名称,@主办单位,@承办单位,@出版单位,@发布日期,@国图分类,@会议地点,@简介,@馆藏号,@登录日期)";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", zwhy.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("定期会议", zwhy.dqhy));
            sqlcmd.Parameters.Add(new OleDbParameter("索引编码", zwhy.sybm));
            sqlcmd.Parameters.Add(new OleDbParameter("年份", zwhy.nf));
            sqlcmd.Parameters.Add(new OleDbParameter("文种", zwhy.wz));
            sqlcmd.Parameters.Add(new OleDbParameter("中文名称", zwhy.zwmc));
            sqlcmd.Parameters.Add(new OleDbParameter("外文名称", zwhy.wwmc));
            sqlcmd.Parameters.Add(new OleDbParameter("主办单位", zwhy.zbdw));
            sqlcmd.Parameters.Add(new OleDbParameter("承办单位", zwhy.cbdw));
            sqlcmd.Parameters.Add(new OleDbParameter("出版单位", zwhy.chbdw));
            sqlcmd.Parameters.Add(new OleDbParameter("发布日期", zwhy.fbrq));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zwhy.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("会议地点", zwhy.hydd));
            sqlcmd.Parameters.Add(new OleDbParameter("简介", zwhy.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("馆藏号", zwhy.gch));
            OleDbParameter parameter = new OleDbParameter("登陆日期", OleDbType.DBDate);
            parameter.Value = zwhy.dlrq;
            sqlcmd.Parameters.Add(parameter);
            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();

            this.closeOleDbConnection();
            return err;
        }

        public override int deletePM(PM pm, out String err)
        {
            int back = 0;
            err = String.Empty;
            ZWHY_PM zwhy = (ZWHY_PM)pm;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            if (!String.Empty.Equals(validateTableName(formatPmTableName)))
            {
                sqlcmd.CommandText = @"create Table " + formatPmTableName + @"(索引编码 char(20), 文件名 char(30), 中文篇名 char(255), 外文篇名 char(255), 作者 char(255), 作者单位 char(255), 中文文摘 text, 外文文摘 text, 中文主题词 char(255), 外文主题词 char(255), 页数 smallInt, 国图分类 char(30), 馆藏号 char(250), 原文件名 char(250), 登录人 char(4), 更新日期 datetime), 更新项 char(10)";
                sqlcmd.Connection = this.ObjConnection;
                try
                {
                    back = sqlcmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    err = e.Message + "\n";
                    return 0;
                }
            }
            //sql语句
            sqlcmd.CommandText = @"delete from " + formatPmTableName + " where 索引编码=@索引编码 and 中文篇名=@中文篇名";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("索引编码", zwhy.sybm));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwhy.zwpm));

            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = "Exception: " + e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();
            //            this.closeOleDbConnection();
            return back;
        }
        public override int deleteSyml(SYML syml, out String err)
        {
            int back = 0;
            err = String.Empty;
            ZWHY_SYML zwhy = (ZWHY_SYML)syml;
            //打开连接
            this.openOleDbConnection();
            OleDbCommand sqlcmd = new OleDbCommand();
            //sql语句
            sqlcmd.CommandText = @"delete from " + symlTableName + " where 分类编码=@分类编码 and 中文名称=@中文名称 and 馆藏号=@馆藏号";
            sqlcmd.Connection = this.ObjConnection;
            sqlcmd.Parameters.Add(new OleDbParameter("分类编码", zwhy.flbm));
            sqlcmd.Parameters.Add(new OleDbParameter("中文名称", zwhy.zwmc));
            sqlcmd.Parameters.Add(new OleDbParameter("馆藏号", zwhy.gch));

            try
            {
                back = sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = "Exception: " + e.Message + "\n";
            }
            sqlcmd.Parameters.Clear();
            //            this.closeOleDbConnection();
            return back;
        }

        /* 现在的方法先delete再insert，就不用为每文库写update了
        public override bool updatePM(PM pm)
        {
            ZWQK_PM zwqk = (ZWQK_PM)pm;
            //打开连接
            this.openOleDbConnection();

            //sql语句
            OleDbCommand sqlcmd = new OleDbCommand(@"update ZWQK set 作者=@作者,作者单位=@作者单位,中文主题词=@中文主题词,外文主题词=@外文主题词,国图分类=@国图分类 where 中文篇名=@中文篇名 and 文件名=@文件名", this.ObjConnection);

            sqlcmd.Parameters.Add(new OleDbParameter("作者", zwqk.zz));
            sqlcmd.Parameters.Add(new OleDbParameter("作者单位", zwqk.zzdw));
            sqlcmd.Parameters.Add(new OleDbParameter("中文主题词", zwqk.zwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("外文主题词", zwqk.wwztc));
            sqlcmd.Parameters.Add(new OleDbParameter("国图分类", zwqk.gtfl));
            sqlcmd.Parameters.Add(new OleDbParameter("中文篇名", zwqk.zwpm));
            sqlcmd.Parameters.Add(new OleDbParameter("文件名", zwqk.wjm));
            int back = sqlcmd.ExecuteNonQuery();
            if (back == 0)
            {
                return false;
            }
            //objConnection.Close();
            return true;
        }*/

        public override bool findServerDuplicate(PM pm)
        {
            ZWHY_PM zwhy = (ZWHY_PM)pm;
            IList<ZWHY_PM> result = zwhyDAO.queryZwhyPM(zwhy.sybm, zwhy.zwpm);
            if (result != null && result.Count != 0)
                return true;
            else
                return false;
        }

        public override bool findServerSymlDuplicate(SYML syml)
        {
            ZWHY_SYML zwhy = (ZWHY_SYML)syml;
            int result = zwhyDAO.countQueryZwhySYML(zwhy.flbm, zwhy.zwmc);
            if (result > 0)
                return true;
            else
                return false;
        }

        public override String insertPMwithUpdateWJM(PM pm)
        {
            String err = String.Empty;
            ZWHY_PM zwhy = (ZWHY_PM)pm;
            ZWHY_PM maxPm = zwhyDAO.getMaxZwhyPM(zwhy.sybm);
            int serial;
            if (maxPm != null)
            {
                int pos = maxPm.wjm.LastIndexOf('-');
                int pos2 = maxPm.wjm.LastIndexOf('.');
                serial = int.Parse(maxPm.wjm.Substring(pos + 1, 4)) + 1;
                String serialStr = "0000" + serial.ToString();
                zwhy.wjm = maxPm.wjm.Substring(0, pos + 1) + serialStr.Substring(serialStr.Length - 4) + maxPm.wjm.Substring(pos2);
            }
            else
            {
                serial = 1;
                int index = zwhy.ywjm.LastIndexOf('.');
                String suffixStr;
                if (index >= 0)
                    suffixStr = zwhy.ywjm.Substring(index);
                else
                    suffixStr = String.Empty;
                String serialStr = "0000" + serial.ToString();
                zwhy.wjm = zwhy.sybm + "-" + serialStr.Substring(serialStr.Length - 4) + suffixStr;
            }
            try
            {
                zwhyDAO.insertZwhyPM(zwhy);
            }
            catch (Exception e)
            {
                err += e.Message + "\n";
            }
            return err;
        }

        public override String insertSymlwithUpdateSybm(SYML syml)
        {
            String err = String.Empty;
            ZWHY_SYML zwhy = (ZWHY_SYML)syml;
            ZWHY_SYML maxSyml = zwhyDAO.getMaxZwhySYML(zwhy.flbm, zwhy.nf);
            int serial;
            if (maxSyml != null)
            {
                int pos = maxSyml.sybm.LastIndexOf('-');
                serial = int.Parse(maxSyml.sybm.Substring(pos + 1, 3)) + 1;
                String serialStr = "0000" + serial.ToString();
                zwhy.sybm = maxSyml.sybm.Substring(0, pos + 1) + serialStr.Substring(serialStr.Length - 3);
            }
            else
            {
                serial = 1;
                String serialStr = "0000" + serial.ToString();
                zwhy.sybm = zwhy.flbm + "-" + zwhy.nf.ToString() + "-" + serialStr.Substring(serialStr.Length - 3);
            }
            try
            {
                zwhyDAO.insertZwhySYML(zwhy);
            }
            catch (Exception e)
            {
                err += e.Message + "\n";
            }
            return err;
        }

        public override string addOneRecord(PM pm)
        {
            String err = String.Empty;
            ZWHY_PM zwhy = (ZWHY_PM)pm;
            zwhy.gxrq = DateTime.Now;
            zwhy.eraseNullInEmptyString();
            //            zwqkDAO.insertZwqkPM(zwqk);
            err += insertPMwithUpdateWJM(pm);
            if (!String.Empty.Equals(err))
                return err;
            String path = "HY/";
            //TODO:目录
            path += zwhy.sybm.Substring(0, 2) + "/";
            path += zwhy.sybm.Substring(2, 2) + "/";
            path += zwhy.sybm.Substring(4, 2) + "/";
            path += zwhy.sybm.Substring(6, 2) + "/";
            path += zwhy.sybm.Substring(9, 4) + "/";

            String srcFile = fileFolder + zwhy.ywjm;
            String destFile = path + zwhy.wjm;
            try
            {
                FTPtransfer.uploadAndCreateDir(destFile, srcFile);
                System.IO.File.Delete(srcFile);
            }
            catch (Exception e)
            {
                err += "Exception: " + e.Message + "\n";
                zwhyDAO.deleteZwhyPMByWjm(zwhy.wjm, user.name);
            }
            return err;
        }

        public override string addOneSymlRecord(SYML syml)
        {
            String err = String.Empty;
            ZWHY_SYML zwhy = (ZWHY_SYML)syml;
            zwhy.dlrq = DateTime.Now;
            zwhy.eraseNullInEmptyString();
            //            zwqkDAO.insertZwqkPM(zwqk);
            err += insertSymlwithUpdateSybm(syml);
            return err;
        }
    }
}